package thread;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 晓星航
 * Date: 2023-07-13
 * Time: 14:57
 */
public class ThreadDemo12 {
    public static void main(String[] args) {
        serial();
        //concurrency();
    }
    //串行执行 一个线程完成
    public static void serial() {
        long beg = System.currentTimeMillis();
        int a = 0;
        for (long i = 0; i < 10_0000_0000; i++) {
            a++;
        }
        int b = 0;
        for (long j = 0; j < 10_0000_0000; j++) {
            b++;
        }
        long end = System.currentTimeMillis();
        System.out.println("执行时间： " + (end - beg) + "ms");
    }
    public static void concurrency(){
        //使用两个线程分别完成自增
        Thread t1 = new Thread(()->{
           long a = 0;
            for (long i = 0; i < 10_0000_0000; i++) {
                a++;
            }
        });
        Thread t2 = new Thread(()->{
            long b = 0;
            for (long i = 0; i < 10_0000_0000; i++) {
                b++;
            }
        });
        //开始计时
        long beg = System.currentTimeMillis();
        t1.start();
        t2.start();
        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //结束计时
        long end = System.currentTimeMillis();
        System.out.println("并发执行时间： " + (end - beg) + "ms");
    }
}
